home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 15 / BBS in a box XV-1.iso / Files / Bus / C / Commander 2.1 Demo .sit / Commander 2.1 Demo / 4D Reference Info Demo.rsrc / TEXT_5508_Using Transactions.txt < prev    next >
Encoding:
Text File  |  1994-11-09  |  6.1 KB  |  119 lines

  1. Using Transactions
  2.  
  3.  
  4. START TRANSACTION
  5. CANCEL TRANSACTION
  6. VALIDATE TRANSACTION
  7.  
  8.  
  9. Transactions are a series of related data modifications that are made to a database. A transaction is not saved permanently to a database until the transaction is validated. If a transaction is not completed, either because it is cancelled or because of some outside event, the modifications are not saved.
  10.  
  11. During a transaction, all changes to the data of a database are stored locally in a temporary buffer. If the transaction is accepted with VALIDATE¬†TRANSACTION, the changes are saved permanently. If the transaction is cancelled with CANCEL¬†TRANSACTION, the changes are not saved.
  12.  
  13. After a transaction is validated or cancelled, the selection of each file for the current process becomes empty because transactions deal with temporary record addresses. For the same reason you should be cautious when using named selections inside a transaction. After a transaction is validated or cancelled, a named selection created before or during the transaction may contain incorrect record addresses. For instance, a named selection may contain the address of a deleted record or the temporary address of a record added during the transaction.
  14.  
  15. This applies to sets also because they are based on bit tables with record addresses.
  16.  
  17.  
  18. Transaction Example
  19.  
  20. The example in this section is based on the database structure shown below. The database is a simple invoicing system. The invoice lines are stored in a file called [Invoice Lines] which is related to the file [Invoices] by the means of a relation between the fields [Invoices]Invoice ID and [Invoice Lines]Invoice ID. When an invoice is added, a unique ID is calculated using the Sequence¬†number command. The relation between [Invoices] and [Invoices Lines] is an automatic Relate many relation. The Auto Assign Related Value check box is checked.
  21.  
  22. The relation between [Invoice Lines] and [Parts] is manual.
  23.  
  24. When a user enters an invoice, the following actions have to be executed:
  25.  
  26. ‚Ä¢ Add a record in the file [Invoices].
  27.  
  28. ‚Ä¢ Add several records in the file [Invoice Lines].
  29.  
  30. ‚Ä¢ Update the [Parts]ln Warehouse field of each part listed in the invoice.
  31.  
  32.  
  33. In other words, you need to save related data. It is a typical situation where you need to use a transaction.
  34.  
  35. You need to be sure that you can save all these records during the operation or to be able to cancel the transaction if a record cannot be added or updated.
  36.  
  37. If you do not use a transaction, you cannot guarantee the logical data integrity of your database. For instance, if one record of the [Parts] records is locked you will not be able to update the quantity stored in the field [Parts]In Warehouse. Therefore this field will become logically incorrect. The sum of the parts sold and the parts remaining in the warehouse will not be equal to the original quantity entered in the record. You can avoid such a situation by using transactions.
  38.  
  39.  
  40. The layout has two buttons: bCancel and bOK. Both buttons are no action buttons and are managed in the layout procedure.
  41.  
  42. The procedure below sets the read-write/read-only states for the files. Then it creates a loop to add new records:
  43.  
  44. READ ONLY([Invoices])              ` Set the state for the file
  45. READ WRITE([Invoices Lines])     ` Set read-write for this file
  46. READ ONLY([Parts])                  ` Set read-only for this file
  47. INPUT LAYOUT([Invoices];"Input")   ` Select the good layout
  48. Repeat
  49.     ADD RECORD([Invoices])      ` Add as many records as required
  50. Until (bOK=0)
  51. READ ONLY([Invoice Lines])     ` Set the state for the files
  52.  
  53.  
  54.  
  55.  
  56. The transaction is managed in the layout procedure listed below:
  57.  
  58. Case of
  59.   : (Before)          ` Before the layout appears
  60.         START TRANSACTION   ` Before data entry start transaction
  61.         [Invoices]Invoice ID:=Sequence number([Invoices]Invoice ID)
  62.  
  63.    : (During)                 ` During the data entry
  64.         [Invoices]Total Invoice:=Sum([Invoice Lines]Total line)
  65.  
  66.         Case of
  67.          :(bCancel=1)      ` You clicked on Cancel
  68.               CANCEL TRANSACTION      ` Cancel everything
  69.               CANCEL    ` Leave the layout
  70.   
  71.          :(bValidate=1)   `  You clicked on validate
  72.                $NbLines:=Records in selection([Invoice Lines])
  73.                READ WRITE([Parts])          `  Update Parts
  74.                FIRST RECORD([Invoice Lines])       ` Start at the first line
  75.                              ` Assume everything will be OK
  76.  
  77.                For ($Line; 1 ;$NbLines)            `  For each line
  78.                   RELATE ONE([Invoice Lines]Part No)
  79.                   OK:=1               `  Assume you want to continue
  80.  
  81.                   While (Locked([Parts]) & (OK=1 ))
  82.                      CONFlRM("The Part "+[Invoice Lines]Part No+" is in 
  83.                                                                                          use. Wait?")
  84.                      If (OK=1)
  85.                         DELAY PROCESS(Current process;60)
  86.                   LOAD RECORD([Parts])   ` Try to load the record
  87.                     End if
  88.              End while
  89.  
  90.                  If (OK=1)
  91.                    [Parts]In Warehouse:=[Parts]In Warehouse-[lnvoice 
  92.                                                                                        Lines]Quantity
  93.                         `  Update quantity in the warehouse
  94.                    SAVE RECORD([Parts])       `  Save the record
  95.                 Else
  96.                    $Line:=$NbLines+1
  97.                    $ValidTrans:=False
  98.                 End if
  99.  
  100.                NEXT RECORD([Invoice Lines]) `Go next line
  101.           End for
  102.  
  103. READ ONLY([Parts])
  104.  
  105. If ($ValidTrans)
  106.     SAVE RECORD([Invoices])
  107.     VALIDATE TRANSACTION
  108. Else
  109.     CANCEL TRANSACTION 
  110. End if 
  111.  
  112. CANCEL 
  113.  
  114.  
  115.  
  116. In the procedure above regardless of the button clicked, we execute the ¬†CANCEL. The new record is not validated by a call to ¬†ACCEPT but by the SAVE¬†RECORD command. As you can see the SAVE¬†RECORD is called just before the VALIDATE¬†TRANSACTION command. Therefore, saving the [Invoices] record is actually a part of the transaction. Calling the ¬†ACCEPT command would also validate the record but in this case the transaction would be validated before the Invoice was saved. In other words, the record would be saved outside the transaction.
  117.  
  118.  
  119.